<template>
  <yk-space dir="vertical" style="width: 100%;" size="xl">
    <articleItem v-for="item in articleList" :key="item.id" :data="item" @delete="deleteArticle(item.id)"
      @state="updateState" :serchTerm="props.serchTerm">
    </articleItem>
    <div class="pagination" v-show="count / props.pageSize > 1">
      <yk-pagination size="m" :total="count" @change="changePage" :default-page-size="props.pageSize" />
    </div>
    <div class="empty" v-show="count === 0">
      <yk-empty description="瞅啥呢，没数据" type="secondary"></yk-empty>
    </div>
  </yk-space>
</template>

<script setup lang="ts">
import { onMounted, watch } from 'vue';
import articleItem from './articleItem.vue';
import { useUserStore } from '@/stores/user';
import { useArticle } from '@/hooks/article';
const { getData, articleList, count, updateState, deleteArticle } = useArticle()
const userStore = useUserStore()
const props = defineProps({
  pageSize: {
    type: Number,
    default: 3
  },
  subsetId: {
    default: -1,
    type: [Number, String]
  },
  state: {
    default: -1,
    type: [Number, String]
  },
  serchTerm: {
    default: '',
    type: String
  }
})

const request = {
  token: userStore.token,//用户token
  pageSize: props.pageSize,//单页条数；
  nowPage: 1,//当前页
  state: props.state,//状态
  subsetId: props.subsetId,//分组
  serchTerm: props.serchTerm,//搜索词条
  count: true,
  classify: 0, // 分类，默认为0
}





// 分页查询
const changePage = (e: number) => {
  request.nowPage = e
  getData(request)
}

watch(
  props, () => {
    console.log('props', props.serchTerm);
    // 查询数据
    request.nowPage = 1; // 重置当前页
    request.state = props.state; // 更新状态
    request.subsetId = props.subsetId; // 更新分组ID
    request.serchTerm = props.serchTerm;
    getData(request);
  }
)

onMounted(() => {
  getData(request);

})
</script>

<style scoped lang="less">
// 颜色变量
@primary-color: #3a7bd5;
@primary-light: rgba(58, 123, 213, 0.08);
@text-color: #2c3e50;
@text-light: #5e6c84;
@bg-color: #ffffff;
@bg-light: #f9fafc;
@border-color: #e8ecf1;
@shadow-color: rgba(0, 0, 0, 0.05);
@transition: all 0.3s cubic-bezier(0.25, 1, 0.5, 1);

// 文章列表容器
.yk-space {
  position: relative;

  &::after {
    content: '';
    position: absolute;
    left: 0;
    top: -8px;
    width: 60px;
    height: 3px;
    background: linear-gradient(to right, @primary-color, lighten(@primary-color, 15%));
    border-radius: 2px;
    opacity: 0.8;
  }
}

// 分页导航
.pagination {
  padding: 20px 0 30px;
  display: flex;
  align-items: center;
  justify-content: center; // 居中对齐，更符合博客风格
  width: 100%;
  margin-top: 10px;

  :deep(.yk-pagination) {
    .yk-pagination-item.is-active {
      background-color: @primary-color;
      border-color: @primary-color;
      box-shadow: 0 2px 6px rgba(58, 123, 213, 0.2);

      &:hover {
        background-color: darken(@primary-color, 5%);
      }
    }

    .yk-pagination-item:not(.is-active):hover {
      color: @primary-color;
      border-color: @primary-color;
    }

    .yk-pagination-prev,
    .yk-pagination-next {
      &:hover {
        color: @primary-color;
        border-color: @primary-color;
      }
    }
  }
}

// 空数据状态
.empty {
  width: 100%;
  min-height: 300px;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 20px;
  animation: fadeIn 0.4s ease-out;

  :deep(.yk-empty) {
    opacity: 0.9;

    .yk-empty-image {
      width: 120px;
      height: 120px;
      margin-bottom: 20px;
    }

    .yk-empty-description {
      color: @text-light;
      font-size: 15px;
      font-weight: 500;
    }
  }
}

// 文章项间隙调整
:deep(.yk-space-item) {
  transition: @transition;

  &:hover {
    transform: translateY(-2px);
  }
}

// 添加文章项入场动画
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(15px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

// 为列表项添加动画延迟
:deep(.yk-space-item) {
  opacity: 0;
  animation: fadeIn 0.5s ease-out forwards;

  &:nth-child(1) {
    animation-delay: 0.1s;
  }

  &:nth-child(2) {
    animation-delay: 0.2s;
  }

  &:nth-child(3) {
    animation-delay: 0.3s;
  }

  &:nth-child(4) {
    animation-delay: 0.4s;
  }

  &:nth-child(5) {
    animation-delay: 0.5s;
  }
}

// 响应式样式
@media (min-width: 1200px) {

  // 大屏幕优化
  :deep(.yk-space-item) {
    &:hover {
      transform: translateY(-3px);
    }
  }
}

@media (max-width: 992px) {
  .pagination {
    padding: 16px 0 24px;
  }

  .empty {
    min-height: 260px;

    :deep(.yk-empty) {
      .yk-empty-image {
        width: 100px;
        height: 100px;
      }
    }
  }
}

@media (max-width: 768px) {
  .yk-space {
    &::after {
      width: 50px;
      height: 2px;
    }
  }

  .pagination {
    padding: 12px 0 20px;
    margin-top: 6px;

    :deep(.yk-pagination) {
      .yk-pagination-sizes {
        display: none; // 移动端隐藏每页条数选择器
      }

      .yk-pagination-jumper {
        display: none; // 移动端隐藏页码跳转
      }
    }
  }

  .empty {
    min-height: 220px;
    padding: 30px 15px;

    :deep(.yk-empty) {
      .yk-empty-image {
        width: 90px;
        height: 90px;
        margin-bottom: 16px;
      }

      .yk-empty-description {
        font-size: 14px;
      }
    }
  }

  // 减小动画幅度，提高移动端性能
  :deep(.yk-space-item) {
    &:hover {
      transform: translateY(-1px);
    }
  }
}

@media (max-width: 576px) {
  .pagination {
    padding: 10px 0 16px;

    :deep(.yk-pagination) {
      font-size: 13px;

      .yk-pagination-item,
      .yk-pagination-prev,
      .yk-pagination-next {
        min-width: 28px;
        height: 28px;
        line-height: 28px;
        margin: 0 2px;
      }
    }
  }

  .empty {
    min-height: 180px;
    padding: 20px 10px;

    :deep(.yk-empty) {
      .yk-empty-image {
        width: 80px;
        height: 80px;
        margin-bottom: 12px;
      }

      .yk-empty-description {
        font-size: 13px;
      }
    }
  }
}
</style>
